package com.stone.business.plan.action;

/**
 * <pre>
 * ----计划列表 Action---
 * </pre>
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.plan.pojo.TPlan;
import com.stone.business.plan.service.PlanService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TBdz;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TOrgan;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.exception.AppException;

public class PlanListAction extends BaseListAction {

	private PlanService planService;
	
	public PlanService getPlanService() {
		return planService;
	}

	public void setPlanService(PlanService planService) {
		this.planService = planService;
		this.service=planService;
	}
	
	/** * 构造方法. */
    public PlanListAction() {
        this.entityClass = TPlan.class;
    }
	private String hiddenids;
	private List<TBdz> bdzList;
	private List<TDictionary> dydjList;
	private List<TDictionary> sblxList;
	private long plan_total;
	private long plan_prepared;
	private long plan_unpre;
	private long temp_sctotal;
	private long temp_scprepared;
	private long temp_scunpre;
	private long temp_gltotal;
	private long temp_glprepared;
	private long temp_glunpre;
	 String month;
	 private List list;
	 String currentmonth;
	  String ym;
	  String monthid;
		public String getMonth() {
			return month;
		}

		public void setMonth(String month) {
			this.month = month;
		}

		public String getCurrentmonth() {
			return currentmonth;
		}

		public void setCurrentmonth(String currentmonth) {
			this.currentmonth = currentmonth;
		}

		public String getMonthid() {
			return monthid;
		}

		public void setMonthid(String monthid) {
			this.monthid = monthid;
		}

	/**
	 * 设置查询条件
	 */
	public HashMap<String, Object> setSearchCondition() {
		
		HashMap<String, Object> m = new HashMap<String, Object>();
		List<Object> valsList = new ArrayList<Object>();
		String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
		m.put("cols", "plan");
		//m.put("cols", "new map(s.jhgzsjks as jhgzsjks, s.jhgzsjjs as jhgzsjjs,s.xkgzsj as xkgzsj, s.id as id,s.lx as lx,bdz.bdzname as bdzname, s.bdzname as bdzname, s.ph as ph, s.gzfzrname as gzfzrname, s.gzrw as gzrw,s.qxr as qxr)");
		
		String conditions = "";
		
		// 状态
		if ("0".equals(super.getSearchBean().getCon20())) {
			conditions = " status = '" + super.getSearchBean().getCon20() + "'";
		/*------------modify by fanghf 2012.3.15按状态过滤begin-----------*/
		} else if("1".equals(super.getSearchBean().getCon20())){
			conditions = " status = '1'";
		} else if("3".equals(super.getSearchBean().getCon20())||"4".equals(super.getSearchBean().getCon20())||"5".equals(super.getSearchBean().getCon20())){
			conditions = " status = '9'";
		} else {
			conditions = " status != '0'";
		}
		if (!StringUtils.isNullOrBlank(ym)) {
			conditions = " status != '0'";
		}
		/*------------modify by fanghf 2012.3.15按状态过滤end-----------*/
		// 隔日
		if ("3".equals(super.getSearchBean().getCon20())) {

			conditions += " and to_char(gr,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD')";

		} else if ("4".equals(super.getSearchBean().getCon20())) {

			conditions += " and to_char(dyr,'YYYYMMDD') = to_char(sysdate,'YYYYMMDD')";

		} else if ("5".equals(super.getSearchBean().getCon20())) {

			conditions += " and startdate - trunc(sysdate) = 0";

		}
		/*if ("3".equals(super.getSearchBean().getCon20())) {

			conditions += " and startdate - trunc(sysdate) = 2";

		} else if ("4".equals(super.getSearchBean().getCon20())) {

			conditions += " and startdate - trunc(sysdate) = 1";

		} else if ("5".equals(super.getSearchBean().getCon20())) {

			conditions += " and startdate - trunc(sysdate) = 0";

		}*/
		// conditions += " and s.lx = bdz.id  "; 
			
		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
		if (o.getOrganCode().toString().length() > 6 && !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
			conditions += " and ouid = '" + o.getOrganCode() + "'";
		}
		if (!StringUtils.isNullOrBlank(ym)) {
			conditions = " status != '0'";
		}
		//----------------------查询条件---------------------
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon1())){
			conditions += " AND plan.ouid like ? ";
			valsList.add(organCode+ "%");
		}
		
		// 开始时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())){
			conditions += " AND TO_CHAR(plan.startdate,'yyyy-MM-dd') >= ?";
			valsList.add(super.getSearchBean().getCon2());
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())){
			conditions += " AND TO_CHAR(plan.startdate,'yyyy-MM-dd') <= ?";
			valsList.add(super.getSearchBean().getCon3());
		}
		
		// 班组
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon5())){
			conditions += " AND plan.teamnames like ? ";
			valsList.add("%" + super.getSearchBean().getCon5().replaceAll(",", "%") + "%");
		}
	
		m.put("conditions",conditions);
		m.put("clause"," order by plan.id desc");
		m.put("alias", "plan");//	m.put("alias", "s, TBdz bdz");
		m.put("vals", valsList.toArray());
		
		setDictionary();
		
		return m;
		
	}

	
	// 获得字典项目
	private void setDictionary() {
		
		// 变电站
		this.bdzList = planService.findListByHql(" from TBdz order by sortid");
		// 电压等级
		this.dydjList = planService.findListByHql(" from TDictionary where itemType = 'DYDJ' order by sortid");
		// 电压等级
		this.sblxList = planService.findListByHql(" from TDictionary where itemType = 'SBLX' order by sortid");
	}

	//工作准备 
	public String workPlan(){
//		String sql1 = "";
//		String sql2 = "";
//		String condition1 = " where status = '9'";
//		String condition2 = " where status = '9'";
//		sql1 = "select a.id,A.TEAMNAMES,A.STARTDATE,A.ENDDATE,A.POWERRANGE,A.VOLTAGELEVEL,A.WORKCONTENT,A.STATUS,case when 1=1 then '1' end as datafrom," +
//			   " case when c.id is null then '0' else '1' end as ishavegzd" +
//			   " from tab_plan a left join tab_gzrwd c" +
//			   " on c.planid = a.id";
//		
//		if ("3".equals(super.getSearchBean().getCon20())) {
//			condition1 += " and a.startdate - trunc(sysdate) = 2";
//		}else if("4".equals(super.getSearchBean().getCon20())){
//			condition1 += " and a.startdate - trunc(sysdate) = 1";
//		}else if("5".equals(super.getSearchBean().getCon20())){
//			condition1 += " and a.startdate - trunc(sysdate) = 0";
//		}
//		
//		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
//		if (o.getOrganCode().toString().length() > 6 && !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
//			condition1 += " and a.ouid = '" + o.getOrganCode() + "'";
//		}
//		sql1 += condition1 ;
//		
//		sql2 ="select b.id,B.BZNAME as teamnames,B.STARTDATE,B.ENDDATE,B.TDFW as POWERRANGE,B.DYDJ as VOLTAGELEVEL,B.GZXM as WORKCONTENT,B.STATUS,case when 1=1 then '0' end as datafrom,"
//			+ " case when d.id is null then '0' else '1' end as ishavegzd"
//			+ " from tab_lsap b left join tab_gzrwd d"
//			+ " on d.planid = b.id";
//		
//		if ("3".equals(super.getSearchBean().getCon20())) {
//			condition2 += " and b.startdate - trunc(sysdate) = 2";
//		}else if("4".equals(super.getSearchBean().getCon20())){
//			condition2 += " and b.startdate - trunc(sysdate) = 1";
//		}else if("5".equals(super.getSearchBean().getCon20())){
//			condition2 += " and b.startdate - trunc(sysdate) = 0";
//		}
//		if (o.getOrganCode().toString().length() > 6 && !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
//			condition2 += " and b.ouid = '" + o.getOrganCode() + "'";
//		}
//		sql2 += condition2;
//		
//		this.grgzapList = planService.findListBySql(sql1 + " union " + sql2);
		String sql = "";
		String conditions = " a.status = '9'";
		
		if ("3".equals(super.getSearchBean().getCon20())) {
			conditions += " and a.startdate - trunc(sysdate) = 2";
		} else if ("4".equals(super.getSearchBean().getCon20())) {
			conditions += " and a.startdate - trunc(sysdate) = 1";
		} else if ("5".equals(super.getSearchBean().getCon20())) {
			conditions += " and a.startdate - trunc(sysdate) = 0";
		}
		
		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
		if (o.getOrganCode().toString().length() > 6 && !"1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) {
			conditions += " and a.ouid = '" + o.getOrganCode() + "'";
		}
		//查询总数暂时这样，待修改
		//月度工作计划总数
		sql = "select * from tab_plan a where " + conditions;
		plan_total = getCount(sql);
		
		//月度工作计划已准备条数
		sql = "select * from tab_plan a,tab_gzrwd b where b.planid = a.id and " + conditions;
		plan_prepared = getCount(sql);
		
		//月度工作计划未准备条数
		plan_unpre = plan_total - plan_prepared;
		
		//临时工作安排生产计划总数
		sql = "select * from tab_lsap a where state = '0' and " + conditions;
		temp_sctotal = getCount(sql);
		
		//临时工作安排生产计划已准备条数
		sql = "select * from tab_lsap a,tab_gzrwd b where b.planid = a.id and a.state = '0' and " + conditions;
		temp_scprepared = getCount(sql);
		
		//临时工作安排生产计划未准备条数
		temp_scunpre = temp_sctotal - temp_scprepared;
		
		//临时工作安排管理工作总数
		sql = "select * from tab_lsap a where state = '1' and " + conditions;
		temp_gltotal = getCount(sql);
		
		//临时工作安排管理工作已准备条数
		sql = "select * from tab_lsap a,tab_gzrwd b where b.planid = a.id and a.state = '1' and " + conditions;
		temp_glprepared = getCount(sql);
		
		//临时工作安排管理工作未准备条数
		temp_glunpre = temp_gltotal - temp_glprepared;
		
		return "WORKPLAN";
	}
	
	private int getCount(String sql){
		int count = 0;
		List templist = null;
		templist = planService.findListBySql(sql);
		if(templist != null){
			count = templist.size();
		}
		return count;
	}
	/**
	 * 获得左侧树
	 */
	public String toMonthTree() {
		
		List<HashMap<String, Object>> l = planService.findListBySql("select id as monthid, to_char(startdate, 'YYYY-MM') as currentmonth from tab_plan where TO_CHAR(startdate, 'yyyy-MM') = TO_CHAR(sysdate, 'yyyy-MM')  ");
		
		currentmonth = l.get(0).get("currentmonth").toString();
		monthid = l.get(0).get("monthid").toString();
		
		return "TREE_SUCCESS";
	}
public void getMonthDate() throws Exception {
		
		List<HashMap<String,Object>> listTree;
		
		// 左侧的树
		if ("-1".equals(month)) {
			
			listTree = planService.findListBySql("select to_char(startdate,'YYYY') as text, to_char(startdate,'YYYY') as id, 0 as leaf from tab_plan group by to_char(startdate,'YYYY') order by id");

		// 维护时根节点
		} else if (month.length() == 4) {
			listTree = planService.findListBySql("select to_char(startdate,'MM') as text, '" + month + "' || '-' || to_char(startdate,'MM') as id, 1 as leaf  from tab_plan where to_char(startdate,'YYYY') = '" + month + "' group by to_char(startdate,'MM') order by id");
		} else {
			listTree = planService.findListBySql("select to_char(startdate,'MM') as text, '" + month + "' || '-' || to_char(startdate,'MM') as id, 1 as leaf  from tab_plan where to_char(startdate,'YYYY') = '" + month + "' group by to_char(startdate,'MM') order by id");
			//listTree = myService.findListBySql("select to_char(planweekstart,'DD') || '~' || to_char(planweekend,'DD') as text, id as id, 1 as leaf from tab_plan_week where to_char(planweekstart,'YYYY-MM') = '" + month + "' order by id");
		}
		/*else {
			listTree = myService.findListBySql("select to_char(startdate,'MM') as text, '" + month + "' || '-' || to_char(startdate,'MM') as id, 1 as leaf  from tab_plan_month where to_char(startdate,'YYYY') = '" + month + "' group by to_char(startdate,'MM') order by id");
		}	*/
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(listTree, jsonConfig);
        
        super.getResponse().getWriter().print(json.toString());
	}
/**
 * 进入详细页面
 * @return
 * @throws AppException
 */
public String initDetail() throws AppException {
	
	//getRole();

	// 修改进入
	if (!StringUtils.isNullOrBlank(ym)) {
		list = planService.findListBySql(" select * from TAB_PLAN where to_char(startdate,'YYYY-MM') = '" + ym + "' order by startdate ");

		// 新建进入
	} else {

		list = new ArrayList();

	}

	return SUCCESS;
}
	public List<TBdz> getBdzList() {
		return bdzList;
	}

	public List<TDictionary> getDydjList() {
		return dydjList;
	}

	public List<TDictionary> getSblxList() {
		return sblxList;
	}
	
	public String getHiddenids() {
		return hiddenids;
	}

	public void setHiddenids(String hiddenids) {
		this.hiddenids = hiddenids;
	}

	public long getPlan_total() {
		return plan_total;
	}

	public void setPlan_total(long planTotal) {
		plan_total = planTotal;
	}

	public long getPlan_prepared() {
		return plan_prepared;
	}

	public void setPlan_prepared(long planPrepared) {
		plan_prepared = planPrepared;
	}

	public long getPlan_unpre() {
		return plan_unpre;
	}

	public void setPlan_unpre(long planUnpre) {
		plan_unpre = planUnpre;
	}

	public long getTemp_sctotal() {
		return temp_sctotal;
	}

	public void setTemp_sctotal(long tempSctotal) {
		temp_sctotal = tempSctotal;
	}

	public long getTemp_scprepared() {
		return temp_scprepared;
	}

	public void setTemp_scprepared(long tempScprepared) {
		temp_scprepared = tempScprepared;
	}

	public long getTemp_scunpre() {
		return temp_scunpre;
	}

	public void setTemp_scunpre(long tempScunpre) {
		temp_scunpre = tempScunpre;
	}

	public long getTemp_gltotal() {
		return temp_gltotal;
	}

	public void setTemp_gltotal(long tempGltotal) {
		temp_gltotal = tempGltotal;
	}

	public long getTemp_glprepared() {
		return temp_glprepared;
	}

	public void setTemp_glprepared(long tempGlprepared) {
		temp_glprepared = tempGlprepared;
	}

	public long getTemp_glunpre() {
		return temp_glunpre;
	}

	public void setTemp_glunpre(long tempGlunpre) {
		temp_glunpre = tempGlunpre;
	}

	public void setBdzList(List<TBdz> bdzList) {
		this.bdzList = bdzList;
	}

	public void setDydjList(List<TDictionary> dydjList) {
		this.dydjList = dydjList;
	}

	public void setSblxList(List<TDictionary> sblxList) {
		this.sblxList = sblxList;
	}

	

	
	public void setList(List list) {
		this.list = list;
	}

	public String getYm() {
		return ym;
	}

	public void setYm(String ym) {
		this.ym = ym;
	}
	
}
